<?php

namespace App\Http\Controllers\Admin\notice;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class IndexController extends Controller
{
    //
    public function index(Request $request)
    {
    	//遍历展示
    	$data = DB::table('notice') -> where('title', 'like', '%'.$request -> input('keywords').'%') ->paginate(5);

    	return view('admin.notice.index', ['title' => '公告列表', 'data' => $data, 'request' => $request -> all()]);
    }

    public function add()
    {

    	return view('admin.notice.add', ['title' => '公告添加']);
    }

    public function insert(Request $request)
    {
    	//公告添加
    	$data = $request -> except('_token');

    	$data['time'] = time();

    	$res = DB::table('notice') -> insert($data);

    	if($res)
    	{
    		return redirect('admin/notice/index') -> with(['info' => '添加成功']);
    	}
    	else
    	{
    		return back() -> with(['info' => '添加失败']);
    	}

    }

    //编辑页面
    public function edit($id)
    {

    	$data = DB::table('notice') -> where('id', $id) -> first();

    	return view('admin.notice.edit', ['title' => '公告编辑', 'data' => $data]);
    }

    //修改编辑
    public function update(Request $request,$id)
    {
    	$data = $request -> except('_token');

    	$res = DB::table('notice') -> where('id', $request -> id) -> update($data);

    	if($res)
    	{
    		return redirect('admin/notice/index') -> with(['info' => '修改成功']);
    	}
    	else
    	{
    		return back() -> with(['info' => '修改失败']);
    	}
    }

    //删除
    public function delete($id)
    {
    	$res = DB::table('notice') -> delete($id);

    	if($res)
    	{
    		return redirect('admin/notice/index') -> with(['info' => '删除成功']);
    	}
    	else
    	{
    		return back() -> with(['info' => '删除失败']);
    	}
    }


}
